package br.com.q7solucoes.parsercinemaq7;

import br.com.q7solucoes.parsercinemaq7.URLParser.URLType;
import br.com.q7solucoes.commons.utils.ConfigurationFile;
import br.com.q7solucoes.commons.wordpress.WPPost;
import br.com.q7solucoes.commons.wordpress.WPPostDAO;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

/**
 * Classe que abstrai a configuracao com o arquivo de configuracoes.
 *
 * @author Elenilson Vieira - elenilson[at]q7solucoes.com.br
 * @since 04 de janeiro de 2011
 * @version 1.0
 * @see www.q7solucoes.com.br
 */
public class ServerConfigurationFileWrapper {

    /**
     * Path do arquivo de configuracao.
     */
    public final static String CONFIGURATION_FILE_PATH = "parsercinemaq7.config";
    public final static String VALUES = "parsercinemaq7.config";
    private static ServerConfigurationFileWrapper instance;

    /*** Tags ***/
    public static final String TAG_GOOGLE_MOVIE_URLS = "GOOGLE_MOVIE_URLS";
    public static final String TAG_OTHER_URLS = "OTHER_URLS";
    public static final String TAG_LAST_QUERY = "LAST_QUERY";
    public static final String TAG_DAY_TO_QUERY = "DAY_TO_QUERY";
    public static final String TAG_HOUR_TO_QUERY = "HOUR_TO_QUERY";
    public final static String TAG_EMAIL = "EMAIL";
    public final static String TAG_PASSWORD_EMAIL = "PASSWORD_EMAIL";
    public final static String TAG_MYSQL_DATABASE_HOST = "MYSQL_DATABASE_HOST";
    public final static String TAG_MYSQL_DATABASE = "MYSQL_DATABASE";
    public final static String TAG_MYSQL_DATABASE_USER = "MYSQL_DATABASE_USER";
    public final static String TAG_MYSQL_DATABASE_PASSWORD = "MYSQL_DATABASE_PASSWORD";
    public final static String TAG_MYSQL_DATABASE_CODE = "MYSQL_DATABASE_CODE";
    public final static String TAG_REPLICATE = "REPLICATE";
    
    /**
     * Retorna a unica instance do wrapper.
     *
     * @return a instancia do wrapper
     */
    public static ServerConfigurationFileWrapper getInstance() {
        if (instance == null) {
            instance = new ServerConfigurationFileWrapper();
        }

        return instance;
    }

    public synchronized String[] getGoogleMovieURL() throws FileNotFoundException, IOException {
        try{
            StringTokenizer s =
                    new StringTokenizer(new ConfigurationFile(CONFIGURATION_FILE_PATH)
                    .getTagValue(TAG_GOOGLE_MOVIE_URLS));

            s.nextToken("=");

            return s.toString().split(ConfigurationFile.COMPOST_VALUE_DELIMITER);
        }catch(Exception ex){
            return new String[0];
        }
    }

    public synchronized void setGoogleMovieURL(String urls[]) throws IOException, FileNotFoundException, IllegalAccessException {
        StringBuilder b = new StringBuilder();

        for (String s : urls) {
            b.append(s);
        }

        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_GOOGLE_MOVIE_URLS, b.toString());
    }

    public synchronized URLParser.URLType[] getOtherURLs() throws FileNotFoundException, IOException {
        try{
            String[] preResults = new ConfigurationFile(CONFIGURATION_FILE_PATH)
                    .getTagValue(TAG_OTHER_URLS).split(ConfigurationFile.COMPOST_VALUE_DELIMITER);

            URLParser.URLType[] result = new URLParser.URLType[preResults.length];
            URLType[] values = URLParser.URLType.values();

            for (int i= 0; i < preResults.length; i++) {
                int ordinal = Integer.parseInt(preResults[i].trim());
                result[i] = values[ordinal];
            }

            return result;
        }catch(Exception ex){
            return null;
        }
    }

    public synchronized void setOtherURLs(URLParser.URLType[] othersURLs) throws IOException, FileNotFoundException, IllegalAccessException {
        StringBuilder b = new StringBuilder();

        for (URLType uRLType : othersURLs) {
            b.append(uRLType.ordinal());
        }
        
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_OTHER_URLS, b.toString());
    }

    public synchronized Date getLastQuery() throws FileNotFoundException, IOException {
        try{
            return new Date(new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_LAST_QUERY));
        }catch(IllegalArgumentException ex){
            return GregorianCalendar.getInstance().getTime();
        }
    }

    public synchronized void setLastQuery(Date lastQuery) throws IOException, FileNotFoundException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_LAST_QUERY, lastQuery.toLocaleString());
    }

    public synchronized int getDayToQuery() throws FileNotFoundException, IOException {
        try{
            return Integer.parseInt(new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_DAY_TO_QUERY));
        }catch(NumberFormatException ex){
            return 5;
        }
    }

    public synchronized void setDayToQuery(int day) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_DAY_TO_QUERY, day + "");
    }

    public synchronized int getHourToQuery() throws FileNotFoundException, IOException {
        try{
            return Integer.parseInt(new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_HOUR_TO_QUERY));
        }catch(NumberFormatException ex){
            return 14;
        }
    }

    public synchronized void setHourToQuery(int hour) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_HOUR_TO_QUERY, hour + "");
    }

    public String getMysqlDatabasePassword() throws FileNotFoundException, IOException {
        return new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_MYSQL_DATABASE_PASSWORD);
    }

    public void setMysqlDatabasePassword(String passwordMysqlDatabase) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_MYSQL_DATABASE_PASSWORD, passwordMysqlDatabase);
    }

    public String getMysqlDatabaseHost() throws FileNotFoundException, IOException {
        return new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_MYSQL_DATABASE_HOST);
    }

    public void setMysqlDatabaseHost(String mysqlDatabaseHost) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_MYSQL_DATABASE_HOST, mysqlDatabaseHost);
    }

    public String getMysqlDatabase() throws FileNotFoundException, IOException {
        return new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_MYSQL_DATABASE);
    }

    public void setMysqlDatabase(String mysqlDatabase) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_MYSQL_DATABASE, mysqlDatabase);
    }

    public String getMysqlDatabaseUser() throws FileNotFoundException, IOException {
        return new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_MYSQL_DATABASE_USER);
    }

    public void setMysqlDatabaseUser(String mysqlDatabaseUser) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_MYSQL_DATABASE_USER, mysqlDatabaseUser);
    }

    public String getMysqlDatabaseCode() throws FileNotFoundException, IOException {
        return new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_MYSQL_DATABASE_CODE);
    }

    public void setMysqlDatabaseCode(String code) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_MYSQL_DATABASE_CODE, code);
    }

    public synchronized String getEmail() throws FileNotFoundException, IOException {
        return new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_EMAIL);
    }

    public synchronized void setEmail(String email) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_EMAIL, email);
    }

    public synchronized String getPasswordEmail() throws FileNotFoundException, IOException {
        return new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_PASSWORD_EMAIL);
    }

    public synchronized void setPasswordEmail(String passwordEmail) throws FileNotFoundException, IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_PASSWORD_EMAIL, passwordEmail);
    }

    public synchronized boolean isReplicate() {
        try{
            return Boolean.parseBoolean(new ConfigurationFile(CONFIGURATION_FILE_PATH).getTagValue(TAG_REPLICATE));
        }catch(Exception ex){
            return false;
        }
    }

    public synchronized void setReplicate(boolean replicate) throws IOException, IllegalAccessException {
        new ConfigurationFile(CONFIGURATION_FILE_PATH).setTagValue(TAG_REPLICATE, replicate + "");
    }
}
